@page "/ClusterState"
@using MudBlazor
@using Proto.Cluster.Dashboard.Shared
@using Proto.Cluster.Gossip
@using Proto.Remote
@inject ActorSystem System
<PageTitle>Cluster State</PageTitle>

<MemberSearch OnMemberSelected="MemberSelected" OnGossipState="GossipState"></MemberSearch>

<MudDrawer @bind-Open="@_open" Anchor="Anchor.Right" Variant="DrawerVariant.Temporary" ClipMode="DrawerClipMode.Never" Elevation="2" Width="80%" >
    @if (_state != null && _member != null)
    {
        var memberState = _state.Members[_member.Id];
        <div class="pa-0">
        <MemberCard Outlined="false" Square="true" Member="_member"></MemberCard>
        </div>
        <MudTabs Rounded="false" Color="Color.Secondary" SliderColor="Color.Info">
            <MudTabPanel Text="State">
                <div Class="pa-2">
                    <MemberStateWidget MemberId="@_member.Id" State="@memberState"></MemberStateWidget>
                </div>
            </MudTabPanel>
            <MudTabPanel Text="Actors">
                <MudCard Square>
                    <MudCardHeader>
                        
                        <MudTextField Adornment="Adornment.Start"
                                      AdornmentIcon="@Icons.Filled.Search"
                                      Label="Search"
                                      Variant="Variant.Outlined"
                                      @bind-Value="_search"
                                      TextChanged="SearchActors"
                                      />
                        
                    </MudCardHeader>
                                        
                </MudCard>
                
                <div Class="pa-2">
                    <MudPaper Elevation="2" Class="my-2" Style="max-height: 300px;overflow-y: scroll">
                        <MudList Clickable="true" Dense>
                            @if (_pids != null)
                            {
                                foreach (var pid in _pids)
                                {
                                    <MudListItem Text="@pid.Id" OnClick="() => ClickPid(pid)"/>
                                }
                            }
                        </MudList>
                    </MudPaper>
                    @if (_pid != null)
                    {
                        <MudPaper Class="pa-2 my-2">
                            <MudText Typo="Typo.h5">Diagnostics for PID: @_pid.Id</MudText>
                            <MudAlert Severity="Severity.Info" NoIcon>
                                @_diagnostics
                            </MudAlert>
                        </MudPaper>
                    }
                </div>
            </MudTabPanel>
        </MudTabs>
    }
    else
    {
        <span>null data</span>
    }
</MudDrawer>


@code {
    private Member? _member;
    private GossipState? _state;
    private string _search = "";
    private bool _open = false;
    private PID[] _pids= null!;
    private PID _pid= null!;
    private string _diagnostics= null!;

    private Task MemberSelected(Member member)
    {
        _member = member;
        _open = true;
        return Task.CompletedTask;
    }

    private Task GossipState(GossipState gossipState)
    {
        _state = gossipState;
        return Task.CompletedTask;
    }

    private async Task SearchActors()
    {
        var client = GetClient();
        var res = await client.ListProcessesAsync(new ListProcessesRequest() { Pattern = _search , Type = ListProcessesMatchType.MatchPartOfString});
        _pids = res.Pids.OrderBy(p => p.Id).ToArray();
    }

    private Remoting.RemotingClient GetClient()
    {
        var channel = Grpc.Net.Client.GrpcChannel.ForAddress("http://" + _member!.Address);
        var client = new Remoting.RemotingClient(channel);
        return client;
    }

    private async Task ClickPid(PID pid)
    {
        _pid = pid;
        _diagnostics = "";
        var client = GetClient();
        var res = await client.GetProcessDiagnosticsAsync(new GetProcessDiagnosticsRequest() { Pid = pid });
        Console.WriteLine(res.DiagnosticsString);
        _diagnostics = res.DiagnosticsString;
    }
}